package org.swimv2.servlet;

import java.io.IOException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneLogin;
import org.swimv2.controller.GestioneLoginRemote;
import org.swimv2.exception.InvalidLoginException;
import org.swimv2.model.Persona;





/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 5909797442154638761L;

	/**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		try {
			/*ContextUtil o simile,funziona come l'omonimo dell'example3*/
			// vedere bene cosa fa lookup!!!!
			//Object obj = ContextUtil.getInitialContext().lookup("ManagerUtenti/remote");
			//GestioneLoginRemote manager = (GestioneLoginRemote) PortableRemoteObject.narrow(obj, GestioneLoginRemote.class);
			
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneLogin.JNDI_NAME);
			GestioneLoginRemote manager = (GestioneLoginRemote)PortableRemoteObject.narrow(obj,GestioneLoginRemote.class);
			
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			RequestDispatcher disp;
			// Utente ha un metodo getRuolo per l'enum ruolo (UTENTE o AMMINISTRATORE)
			try{
			Persona u = manager.validaLogin(username, password);
			
			request.getSession().setAttribute("utente",u);
			
			if(u.getRuolo().equals("UTENTE")) {
				disp = request.getRequestDispatcher("/PaginaProfiloServlet");
			}  
			
			else { 
			//AMMINISTRATORE
				disp = request.getRequestDispatcher("WEB-INF/paginaAmministratore.jsp");
			}
			}catch (InvalidLoginException e){
				disp = request.getRequestDispatcher("WEB-INF/home.jsp");
				request.setAttribute("messaggio", "Errore: codice utente o password errati.");
			}
			
			disp.forward(request, response);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
	}

}
